package com.baizhi.controller;


import com.baizhi.domain.Result;
import com.baizhi.domain.StatusCode;
import com.baizhi.entity.CmfzBanner;
import com.baizhi.service.CmfzBannerService;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ljh
 * @since 2020-08-18
 */
@RestController
@RequestMapping("/cmfzBanner")
public class CmfzBannerController {

    @Autowired
    private CmfzBannerService cmfzBannerService;

    @Value("${imgPath}")
    private String imgPath;

    /**
     * 分页查询轮播图
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/selectAllBannerByPage")
    public Map<String,Object> selectAllBannerByPage(Integer page, Integer limit){
        HashMap<String, Object> map = new HashMap<>();
        try{
            PageInfo<CmfzBanner> pageInfo=cmfzBannerService.selectAllBannerByPage(page,limit);
            System.out.println(pageInfo.getList());
            map.put("code",0);
            map.put("count",pageInfo.getTotal());
            map.put("data",pageInfo.getList());
            map.put("msg","查询成功");
        }catch (Exception e){
            map.put("code",1);
            map.put("msg","查询失败");
        }
        return map;
    }

    /**
     * 上传头像
     * @param file
     * @return
     */
    @RequestMapping("/uploadBannerImg")
    public Map<String,Object> uploadBannerImg(MultipartFile file){
        HashMap<String, Object> map = new HashMap<>();
        try {
            file.transferTo(new File(imgPath,file.getOriginalFilename()));
            map.put("code",0);
            map.put("msg","上传成功");
            map.put("path",file.getOriginalFilename());
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 新增banner
     * @param cmfzBanner
     * @return
     */
    @RequestMapping("/addBanner")
    public Result addBanner(CmfzBanner cmfzBanner){
        try {
            cmfzBanner.setBannerDate(new Date());
            cmfzBanner.setBannerState(0);
            cmfzBannerService.addBanner(cmfzBanner);
            return new Result(true, StatusCode.OK,"新增成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"新增失败");
        }
    }
    /**
     * 修改banner
     * @param cmfzBanner
     * @return
     */
    @RequestMapping("/updateBanner")
    public Result updateBanner(CmfzBanner cmfzBanner){
        try {
            System.out.println(cmfzBanner);
            cmfzBannerService.updateBanner(cmfzBanner);
            return new Result(true, StatusCode.OK,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"修改失败");
        }
    }


    /**
     * 逻辑删除
     * @return
     */
    @RequestMapping("/deleteBanner")
    public Result deleteBanner(Integer id){
        try {
            cmfzBannerService.deleteBanner(id);
            return new Result(true, StatusCode.OK,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"删除失败");
        }
    }


}
